// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
      userNick:'',
      avatarUrl:'',
      username:'',
      password:'',
      switchChecked: false,
      switchStyle: 'text-decoration: line-through'
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad:function(options){
    var that = this;
    console.log("login onload:",getApp().globalData.userInfo)
    // wx.navigateBack({
    //   delta: 0,
    // })
    that.setData({
      userInfo:getApp().globalData.userInfo
    })
  },
  //获取输入账号
  usernameInput:function(e){
    this.setData({
      username:e.detail.value
    })
  },
  //获取输入密码
  passwordInput:function(e){
    this.setData({
      password:e.detail.value
    })
  },
  switchChange:function(e){
      console.log(`switch发生change事件，携带值为`, e.detail.value)
      var obj = {}
      obj[`switchChecked`] = e.detail.value
      this.setData(obj)
      obj = {}
      obj[`switchStyle`] = e.detail.value ? '' : 'text-decoration: line-through'
      this.setData(obj)
  },
  //登陆处理
  login: function(){
    var that = this;
    if(this.data.username.length ==0 || this.data.password.length==0){
      wx.showToast({
        title: '账号密码不为空',
        icon:'error',
        duration:2000
      })
    }else if(this.data.switchChecked == false){
      wx.showToast({
        title: '请勾选协议',
        icon:'error',
        duration:2000
      })
    }else{
      wx.request({
        url:'http://82.157.94.115:2005/login',
        method:'POST',
        data:{
          username:that.data.username,
          password:that.data.password,
        },
        success(res){
          console.log(res)
          console.log('成功')
          if(res.statusCode==200){
             that.setData({
              msg: res.data.msg,
              username: res.data.username ,  //查看输出的console.log(res)数据，确定数据在哪里
              password: res.data.password,
            });
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000
            })
            console.log(that.data.username)
            wx.setStorage({
              key:'loginContent',
              data:that.data.username
            })
            console.log(res.data.msg)

            wx.navigateTo({
              url: '/pages/message/message',
            })
          } else {
            that.setData({
              msg: false,
            })
            wx.showToast({
              title: '账号或密码错误',
              icon: 'error',
              duration: 2000
            })
          }
        }
      })
    }
  },
  toEnroll:function(){
    wx.navigateTo({
      url: '/pages/enroll/enroll',
    })
  },
})
